home *** CD-ROM | disk | FTP | other *** search
/ Beginning Mac Programming / Beginning Mac Programming.bin / pc / Open Me for REALbasic 3 / REALbasic 3.2 / Example Projects / Techniques / Examples by Thomas Tempelmann / TT's MemoryBlock Extension / Source Code (CW Pro 3) / Plugin Source.cpp < prev   
Encoding:
C/C++ Source or Header  |  1999-07-06  |  5.7 KB  |  192 lines

  1. /*
  2.  * Plugin for REALbasic 2 that adds some new methods to RB's MemoryBlock class
  3.  *
  4.  * This code is freeware, you may use and extend it as you like.
  5.  *
  6.  * The accompanying project file was created using CodeWarrior Pro 3.
  7.  *
  8.  * It was written on June 7, 99 by Thomas Tempelmann.
  9.  *
  10.  * More RB-related stuff can be found here: <http://www.tempel.org/rb/>
  11.  *
  12.  * Enjoy and contribute!
  13.  */
  14.  
  15. #include <Memory.h>
  16. #include "rb_plugin.h"
  17.  
  18.  
  19. //----------------
  20. // plugin classes
  21. //----------------
  22.  
  23. extern REALclassDefinition memBlkClass;    // forward declaration
  24.  
  25. static Ptr getMemBlkPtr (REALobject instance)
  26. // Returns the ptr to the actual data of a MemoryBlock
  27. // This is kind of a hack, since I access RB's internal structures directly
  28. // here, but there's no "safer" alternative currently (as of RB 2.0.2)
  29. {
  30.     return *(Ptr*)((Ptr)instance+0x1C);
  31. }
  32.  
  33. static long getSize (REALobject instance)
  34. {
  35.     //DebugStr ("\pSize");
  36.     return *(long*)((Ptr)instance+0x18);
  37. }
  38.  
  39. static REALstring getString (REALobject instance, long offset, long length)
  40. {
  41.     //DebugStr ("\pGetString");
  42.     Ptr memBlk = getMemBlkPtr (instance);
  43.     return REALBuildString (memBlk+offset, length);
  44. }
  45.  
  46. static void setString (REALobject instance, REALstring str, long offset)
  47. {
  48.     //DebugStr ("\pSetString");
  49.     Ptr memBlk = getMemBlkPtr (instance);
  50.     BlockMoveData (str->CString(), memBlk+offset, str->Length());
  51. }
  52.  
  53. static void copyBytes (REALobject instance, long srcOfs, long length, long destOfs)
  54. {
  55.     //DebugStr ("\pCopyBytes");
  56.     Ptr memBlk = getMemBlkPtr (instance);
  57.     BlockMoveData (memBlk+srcOfs, memBlk+destOfs, length);
  58. }
  59.  
  60. static void copyBytes2 (REALobject instance, long srcOfs, long length, REALobject destMemBlk, long destOfs)
  61. {
  62.     //DebugStr ("\pCopyBytes2");
  63.     Ptr memBlk = getMemBlkPtr (instance);
  64.     Ptr memBlk2 = getMemBlkPtr (destMemBlk);
  65.     BlockMoveData (memBlk+srcOfs, memBlk2+destOfs, length);
  66. }
  67.  
  68. static void toMacPtr (REALobject instance, long offset, long length, Ptr dest)
  69. {
  70.     //DebugStr ("\pCopyBytesToMacPtr");
  71.     Ptr memBlk = getMemBlkPtr (instance);
  72.     BlockMoveData (memBlk+offset, dest, length);
  73. }
  74.  
  75. static void fromMacPtr (REALobject instance, Ptr src, long length, long destOfs)
  76. {
  77.     //DebugStr ("\pCopyBytesFromMacPtr");
  78.     Ptr memBlk = getMemBlkPtr (instance);
  79.     BlockMoveData (src, memBlk+destOfs, length);
  80. }
  81.  
  82. static void toMacHdl (REALobject instance, long offset, long length, Handle dest)
  83. {
  84.     //DebugStr ("\pCopyBytesToMacHandle");
  85.     Ptr memBlk = getMemBlkPtr (instance);
  86.     BlockMoveData (memBlk+offset, *dest, length);    // no need to Lock the Handle here, since this operation is atomic and does not move memory by itself
  87. }
  88.  
  89. static void fromMacHdl (REALobject instance, Handle src, long length, long destOfs)
  90. {
  91.     //DebugStr ("\pCopyBytesFromMacHandle");
  92.     Ptr memBlk = getMemBlkPtr (instance);
  93.     BlockMoveData (*src, memBlk+destOfs, length);    // no need to Lock the Handle here, since this operation is atomic and does not move memory by itself
  94. }
  95.  
  96. /* currently unused
  97. static long getPtrSize (REALobject instance)
  98. {
  99.     //DebugStr ("\pPtrSize");
  100.     Ptr memBlk = getMemBlkPtr (instance);
  101.     long l = GetPtrSize (memBlk);
  102.     if (MemError()) {
  103.         return -1;
  104.     } else {
  105.         return l;
  106.     }
  107. }
  108. */
  109.  
  110. /* currently unused
  111. static long getHdlSize (REALobject instance)
  112. {
  113.     //DebugStr ("\pHandleSize");
  114.     Ptr memBlk = getMemBlkPtr (instance);
  115.     long l = GetHandleSize ((Handle)memBlk);
  116.     if (MemError()) {
  117.         return -1;
  118.     } else {
  119.         return l;
  120.     }
  121. }
  122. */
  123.  
  124. #if TARGET_CPU_68K
  125.     typedef Float80 RBdouble;
  126. #else
  127.     typedef double RBdouble;
  128. #endif
  129.  
  130. #if TARGET_CPU_68K
  131.     static void getDouble (RBdouble* f, REALobject instance, long ofs)
  132.     {
  133.         //DebugStr ("\pDoubleValue (get)");
  134.         Ptr memBlk = getMemBlkPtr (instance);
  135.         *f = *(double*)(memBlk+ofs);
  136.     }
  137. #else
  138.     static RBdouble getDouble (REALobject instance, long ofs)
  139.     {
  140.         //DebugStr ("\pDoubleValue (get)");
  141.         Ptr memBlk = getMemBlkPtr (instance);
  142.         return *(double*)(memBlk+ofs);
  143.     }
  144. #endif
  145.  
  146. static void setDouble (REALobject instance, long ofs, RBdouble f)
  147. {
  148.     //DebugStr ("\pDoubleValue (set)");
  149.     Ptr memBlk = getMemBlkPtr (instance);
  150.     *(double*)(memBlk+ofs) = f;
  151. }
  152.  
  153. static REALproperty memBlkClassProperties[] = {
  154.     { nil, "Size", "Integer", 0, (REALproc) getSize },
  155. };
  156.  
  157. static REALmethodDefinition memBlkClassMethods[] = {
  158.     { (REALproc) getDouble, (REALproc) setDouble, "DoubleValue(offset as Integer) as Double"},
  159.     { (REALproc) getString, REALnoImplementation, "GetString(offset as Integer, numBytes as Integer) as String"},
  160.     { (REALproc) setString, REALnoImplementation, "SetString(str as String, offset as Integer)"},
  161.     { (REALproc) copyBytes, REALnoImplementation, "CopyBytes(srcOfs as Integer, numBytes as Integer, destOfs as Integer)"},
  162.     { (REALproc) copyBytes2, REALnoImplementation, "CopyBytes(srcOfs as Integer, numBytes as Integer, destBlk as MemoryBlock, destOfs as Integer)"},
  163.     { (REALproc) toMacPtr, REALnoImplementation, "CopyBytesToMacPtr(srcOfs as Integer, numBytes as Integer, destPtr as Integer)"},
  164.     { (REALproc) fromMacPtr, REALnoImplementation, "CopyBytesFromMacPtr(srcPtr as Integer, numBytes as Integer, destOfs as Integer)"},
  165.     { (REALproc) toMacHdl, REALnoImplementation, "CopyBytesToMacHandle(srcOfs as Integer, numBytes as Integer, destHandle as Integer)"},
  166.     { (REALproc) fromMacHdl, REALnoImplementation, "CopyBytesFromMacHandle(srcHandle as Integer, numBytes as Integer, destOfs as Integer)"},
  167. //    { (REALproc) getPtrSize, REALnoImplementation, "PtrSize() as Integer"},
  168. //    { (REALproc) getHdlSize, REALnoImplementation, "HandleSize() as Integer"},
  169. };
  170.  
  171. static REALclassDefinition memBlkClass = {
  172.     kCurrentREALControlVersion,
  173.     "MemoryBlock",
  174.     nil,
  175.     0,
  176.     0,
  177.     (REALproc) nil,
  178.     (REALproc) nil,
  179.     memBlkClassProperties,
  180.     sizeof(memBlkClassProperties) / sizeof(REALproperty),
  181.     memBlkClassMethods,
  182.     sizeof(memBlkClassMethods) / sizeof(REALmethodDefinition),
  183.     nil,
  184.     0
  185. };
  186.  
  187.  
  188. void PluginEntry (void)
  189. {
  190.     REALRegisterClassExtension (&memBlkClass);
  191. }
  192.